
package com.rt.schedulenew.utils.util;
import java.io.UnsupportedEncodingException;
public class Hex16Util
{
    public static String bytes2HexStr(final String source) {
        byte[] byteArr = new byte[0];
        try {
            byteArr = source.getBytes("utf8");
        }
        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (null == byteArr || byteArr.length < 1) {
            return "";
        }
        final StringBuilder sb = new StringBuilder();
        for (final byte t : byteArr) {
            if ((t & 0xF0) == 0x0) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(t & 0xFF));
        }
        return sb.toString();
    }
    
    public static String hexStr2Bytes(final String hexStr) {
        if (null == hexStr || hexStr.length() < 1) {
            return null;
        }
        final int byteLen = hexStr.length() / 2;
        final byte[] result = new byte[byteLen];
        final char[] hexChar = hexStr.toCharArray();
        for (int i = 0; i < byteLen; ++i) {
            result[i] = (byte)(Character.digit(hexChar[i * 2], 16) << 4 | Character.digit(hexChar[i * 2 + 1], 16));
        }
        try {
            return new String(result, "utf8");
        }
        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "";
        }
    }
}
